package com.itlong.Controller;


import com.itlong.Pojo.Result;
import com.itlong.Pojo.User;
import com.itlong.Server.UserServer;
import com.itlong.Util.JwtUtil;
import com.itlong.Util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

/*    @Autowired
    private Result result;*/
    @Autowired
    private UserServer userServer;

    //用户注册
    @PostMapping("/register")
    public Result getUserByName(String username, String password){
        User u = userServer.getUserByName(username);
        if (u == null){
            //数据库中没有该用户
            //添加用户
            userServer.addUser(username,password);
            return Result.success();
        }
        return Result.error("用户名已被占用");
    }


    //用户登录
    @GetMapping("/login")
    public Result<String> Login(String username, String password){
        //通过用户名来查询用户
        User userLogin = userServer.getUserByName(username);
        if(userLogin == null){
            return Result.error("用户名错误");
        }

        if(userLogin.getPassword().equals(password)){

            Map<String, Object> claims = new HashMap<>();
            claims.put("id", userLogin.getId());
            claims.put("username", userLogin.getUsername());

            String token = JwtUtil.generateToken(claims);

            return Result.success(token);
        }

        return Result.error("密码错误");
    }


    //查询用户信息
    @GetMapping("/finUser")
    public Result<User> FinUser(/*@RequestHeader(name = "Authorization") String token*/){

        /*Map<String, Object> UserToken = JwtUtil.parseToken(token);

        String  username = (String) UserToken.get("username");*/

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");

        User userByName = userServer.getUserByName(username);

        return Result.success(userByName);
    }

    //修改用户信息
    @PutMapping("/update")
    public Result Update(@RequestBody @Validated User user){
//        @RequestBody 作用：是将用户端发送过来的数据映射到相关的对象中
        userServer.update(user);
        return Result.success();

    }

    //更新用户头像
    @PatchMapping("/updatePic")
    public Result UpdateUserPic(@RequestParam String PicUrl){

        userServer.updatePic(PicUrl);
        return Result.success();
    }

    //用户修改密码
    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String, String>  pwd){
        //获取Map中的信息
        //获取旧密码
        String oldPwd = pwd.get("old_pwd");
        //获取新密码
        String newPwd = pwd.get("new_pwd");
        //获取确认密码
        String redPwd = pwd.get("red_pwd");

        //判断填写内容是否为null
        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(redPwd)){
            return Result.error("用户填写的信息不能为空");
        }

        //判断旧密码是否与原密码一样
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userServer.getUserByName(username);
        String password = user.getPassword();
        if(!password.equals(oldPwd)){
            return Result.error("原密码填写错误");
        }

        //判断newPwd与redPwd是否一样
        if(!newPwd.equals(redPwd)){
            return Result.error("两次填写的密码不一样");
        }

        //2.添加新密码
        userServer.updatePwd(newPwd);
        return Result.success();
    }

}
